package vsvm.classifier.optimization;

import java.util.Random;

public class RandomOptimizer extends AbstractOptimizer {
	private int trials;
	
	public RandomOptimizer(int trials) {
		this.trials = trials;				
	}
	
	public double[] optimize(OptimizableParameter[] params, IOptimizable o) {
		int n = params.length;
		//double[] p = new double[n];
		double[] best = new double[n];
		double bestAcc = -1;
		
		Random r = new Random();
		for (int i = 0; i < trials; ++i) {
			for (int j = 0; j < n; ++j) {
				double val = r.nextDouble();
				val = val*(params[j].getEnd() - params[j].getStart()) + params[j].getStart();
				if (params[j].isExponent())
					val = Math.exp(val);
				params[j].setValue(val);
			}
			double res = o.evaluate(params);
			if (res > bestAcc) {
				bestAcc = res;
				for (int k = 0; k < n; ++k)
					best[k] = params[k].getValue();
			}			
		}
		System.out.println("Best acc: " + bestAcc);
		return best;
	}

}
